import DataService from "../../service/Admin/Data.service"
import { Context, Next } from 'koa'

class DataController {
    async downLoadDate(ctx:Context) {
        let data = await DataService.downLoadDate()
        ctx.body = {
            code: 200,
            msg: "获取日期成功",
            data
        }
    }

    // 下载数据
    async getData(ctx:Context) {
        let pageSize = ctx.request.query.pageSize  || 10
        let page = ctx.request.query.page|| 1
        let queryDate = ctx.request.query.queryDate || ''
        let data = await DataService.getDataList(pageSize,page,queryDate)
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data
        }
    }

    //下载传感器的运行数据并且导入成为excel
    async downloadRunData(ctx:any) {
        let date = ctx.request.body.date // 下载的日期
        let result = await DataService.downloadRunData(date) // 下载数据
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data: result
        }
    }

    async getItem(ctx:Context) {
        let date = ctx.request.query.date
        let res = await DataService.getItem(date)

        ctx.body = {
            code: 200,
            msg: "获取成功",
            total: res
        }
    }
    // 保存运行是的数据
    async saveDataList(ctx:any) {
        let data = ctx.request.body
        let runData = {
             module_id: data.module_id,
             uvc:data.uvc,
             inf: data.inf,
             tem: data.tem,
             hum: data.hum,
             pre: data.pre
        }
        let statusData = {
            module_id: data.module_id,
            uvc_sta: data.uvc_sta,
            ir_sta: data.ir_sta,
            temp_sta: data.temp_sta,
            hum_sta: data.hum_sta,
            pre_sta: data.pre_sta
        }
        await DataService.saveDataList(runData)
        await DataService.saveStatusList(statusData)
        ctx.body = {
            code: 200,
            msg: "添加数据成功"
        }
    }
    // 添加运行时间
    async addRunDate(ctx:Context) {
        ctx.body = {
            code: 200,
            msg: "添加运行时间成功"
        }
    }
    async getStatusList(ctx:any) {
        let pageSize = ctx.request.query.pageSize  || 10
        let page = ctx.request.query.page|| 1
        let queryType = ctx.request.query.queryType || ''
        let queryDate = ctx.request.query.queryDate || ''
        let data = await DataService.getStatusDataList(pageSize,page,queryDate,queryType)
        ctx.body = {
            code: 200,
            msg: "获取数据成功",
            data
        }
    }

    async isDel(ctx:Context,next:Next) {
        let bool:boolean = await DataService.isDel()
        if (bool) return await next()
         ctx.body = {
             code: 304,
             msg: "数据量还没到七天，不需要删除"
         }
    }

    // 获取初始化数据值
    async getInitialData(ctx:Context) {
        try {
            const gateway_ip = ctx.request.query.gateway_ip as string;
            const module_id = ctx.request.query.module_id as string;

            if (!gateway_ip || !module_id) {
                ctx.body = {
                    code: 400,
                    msg: "参数错误，需要提供gateway_ip和module_id",
                    data: null
                };
                return;
            }

            const data = await DataService.getInitialData(gateway_ip, module_id);
            ctx.body = {
                code: 200,
                msg: "获取初始化数据成功",
                data
            };
        } catch (error) {
            ctx.body = {
                code: 500,
                msg: "获取初始化数据失败",
                error: "获取初始化数据失败"
            };
        }
    }
    async deleteData(ctx:Context) {
        let result = await DataService.deleteData()
        ctx.body = {
            code: 200,
            msg: "删除成功"
        }
    }
}

export default new DataController